package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsMiddlePictureInfo;
import com.ruoyi.system.domain.news.dto.NewsMiddlePictureInfoDto;
import com.ruoyi.system.domain.news.query.NewsMiddlePictureInfoQuery;
import com.ruoyi.system.domain.news.vo.NewsMiddlePictureInfoVo;
import com.ruoyi.system.service.news.NewsMiddlePictureInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsMiddlePictureInfo")
@Api(value = "newsMiddlePictureInfoController", tags  = "中间图片新闻")
public class NewsMiddlePictureInfoController extends BaseController {

	@Resource
	private NewsMiddlePictureInfoService newsMiddlePictureInfoService;

    @ApiOperation(value = "新增/修改中间图片新闻信息", notes = "新增/修改中间图片新闻信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsMiddlePictureInfoDto newsMiddlePictureInfoDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsMiddlePictureInfo newsMiddlePictureInfo = new NewsMiddlePictureInfo();
		BeanUtils.copyProperties(newsMiddlePictureInfoDto,newsMiddlePictureInfo);
		if (newsMiddlePictureInfo.getId()==null) {
			newsMiddlePictureInfo.setCreateBy(loginUser.getUsername());
			newsMiddlePictureInfo.setDeptId(loginUser.getDeptId());
			newsMiddlePictureInfo.setCreateTime(new Date());
			newsMiddlePictureInfo.setUpdateBy(loginUser.getUsername());
			newsMiddlePictureInfo.setUpdateTime(new Date());
		} else {
			newsMiddlePictureInfo.setUpdateBy(loginUser.getUsername());
			newsMiddlePictureInfo.setUpdateTime(new Date());
		}
		boolean result = newsMiddlePictureInfoService.saveOrUpdate(newsMiddlePictureInfo);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除中间图片新闻信息", notes = "删除中间图片新闻信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsMiddlePictureInfoService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个中间图片新闻信息", notes = "获取单个中间图片新闻信息")
	@GetMapping("/getById")
	public R<NewsMiddlePictureInfoVo> getById(@RequestParam(value = "id") Long id) {
		NewsMiddlePictureInfoQuery newsMiddlePictureInfoQuery = new NewsMiddlePictureInfoQuery();
		newsMiddlePictureInfoQuery.setId(id);
		List<NewsMiddlePictureInfoVo> newsMiddlePictureInfoList = newsMiddlePictureInfoService.getNewsMiddlePictureInfoList(newsMiddlePictureInfoQuery);
		if (newsMiddlePictureInfoList!=null&&newsMiddlePictureInfoList.size()>0) {
			return R.ok(newsMiddlePictureInfoList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取中间图片新闻信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsMiddlePictureInfoVo>> list(@RequestBody NewsMiddlePictureInfoQuery newsMiddlePictureInfoQuery) {
		List<NewsMiddlePictureInfoVo> newsMiddlePictureInfoList = newsMiddlePictureInfoService.getNewsMiddlePictureInfoList(newsMiddlePictureInfoQuery);
		return R.ok(new PageInfo<>(newsMiddlePictureInfoList));
	}
}
